#!/bin/bash
 

#全局变量
SRC_PATH=~/Src
SYS_ROOT_PATH=/opt/riscv64-glibc-gcc-thead_20200702/sysroot
CROSS_PREFIX=/opt/riscv64-glibc-gcc-thead_20200702/bin/riscv64-unknown-linux-gnu-


######################## OPEN SSL ##########################
#openssl
OPENSSL_SRC_PATH=${SRC_PATH}/openssl
OPENSSL_INSTALL_PATH=/opt/riscv64-glibc-gcc-thead_20200702/sysroot/usr
OPENSSL_DIR=/opt/riscv64-glibc-gcc-thead_20200702/sysroot/usr

# 版本
sll_ver=1.1.1g
# 3.0以上_用.代替
sll_ver_url=1_1_1g

#openssl 解压的目录名称
OPENSSL_PACKAGE_NAME=openssl-${sll_ver}
#openssl 压缩包名称
OPENSSL_COMPRESS_PACKAG=openssl-${sll_ver}.tar.gz
#openssl 下载链接
ssl_download_url=https://github.com/openssl/openssl/releases/download/OpenSSL_${sll_ver_url}/${OPENSSL_COMPRESS_PACKAG}

#创建src文件夹
do_make_src_dir()
{
    if [ ! -d "${SRC_PATH}" ];then
        echo -e "\033[1;33mmkdir ${SRC_PATH} ...\033[0m"
        mkdir ${SRC_PATH}
    fi
}

#
do_download_openssl () {
    echo -e "\033[1;33mstart make and install download_openssl ...\033[0m"
    if [ ! -d "${SRC_PATH}" ] ; then
        mkdir ${SRC_PATH}
    fi
    
    if [ ! -d "${OPENSSL_SRC_PATH}" ];then
        mkdir ${OPENSSL_SRC_PATH}
    fi
    
    cd ${OPENSSL_SRC_PATH}
    
    if [ ! -f "${OPENSSL_COMPRESS_PACKAG}" ];then
        wget -c ${ssl_download_url}
    fi
    
    if [ ! -d "${OPENSSL_PACKAGE_NAME}" ];then
        tar -xf ${OPENSSL_COMPRESS_PACKAG}
    fi
    
    echo -e "\033[1;33m  done...\033[0m"
}

do_config_openssl () {
    echo -e "\033[1;33mstart make and install config ...\033[0m"
    cd ${OPENSSL_SRC_PATH}/${OPENSSL_PACKAGE_NAME}
    export CFLAGS=--sysroot=${SYS_ROOT_PATH}
    export CXXFLAGS=--sysroot=${SYS_ROOT_PATH}
    # export CNF_CFLAGS=-pthread
    # export CNF_CXXFLAGS=-std=c++11 -pthread
    ./config shared no-asm --cross-compile-prefix=${CROSS_PREFIX} --prefix=${OPENSSL_INSTALL_PATH} --openssldir=${OPENSSL_DIR}
    
    echo -e "\033[1;33m remove ${OPENSSL_SRC_PATH}/${OPENSSL_PACKAGE_NAME}/makefile -m64 ...\033[0m"
    echo -e "\033[1;33m 删除-m64 CFLAGS ...\033[0m"
    echo -e "\033[1;33m sed -i 's/-m64//g' ${OPENSSL_SRC_PATH}/${OPENSSL_PACKAGE_NAME}/Makefileg ...\033[0m"
    sed -i 's/-m64//g' ${OPENSSL_SRC_PATH}/${OPENSSL_PACKAGE_NAME}/Makefile
    echo -e "\033[1;33m  done...\033[0m"
}

do_make_openssl () {
    echo -e "\033[1;33mstart 开始编译 ...\033[0m"
    cd ${OPENSSL_SRC_PATH}/${OPENSSL_PACKAGE_NAME}
    time(make -j6 && make install -j6)
    echo -e "\033[1;33m  done...\033[0m"
}



######################## QT ##########################

QT_PLATFORM=my-linux-risc-qt
Qt_SRC_PATH=${SRC_PATH}/Qt


#PKG_QT_CONFIG_PATH=${SYS_ROOT_PATH}/usr/lib64
#OPENSSL_PATH=/opt/riscv64-glibc-gcc-thead_20200702/sysroot/usr
#修改源码包解压后的名称
# MAJOR_NAME=qt-everywhere-src

#修改需要下载的源码前缀和后缀
OPENSRC_VER_PREFIX=5.15
OPENSRC_VER_SUFFIX=.16


# #添加alsa交叉编译的动态库文件和头文件路径
# ALSA_LIB=/opt/alsa-lib-1.2.2/lib
# ALSA_INC=/opt/alsa-lib-1.2.2/include

#修改源码包解压后的名称
# QT_PACKAGE_NAME=${MAJOR_NAME}-${OPENSRC_VER_PREFIX}${OPENSRC_VER_SUFFIX}
# QT_PACKAGE_NAME=qtbase-5.15.16-lts-lgpl
QT_PACKAGE_NAME=qtbase-everywhere-src-5.15.16
#定义编译后安装--生成的文件,文件夹位置路径
QT_INSTALL_PATH=/opt/Qt5

#定义压缩包名称
# QT_COMPRESS_PACKAGE=${QT_PACKAGE_NAME}.tar.xz
# QT_COMPRESS_PACKAGE=v5.15.16-lts-lgpl.tar.gz
QT_COMPRESS_PACKAGE=qtbase-everywhere-opensource-src-5.15.16.tar.xz
#无需修改--自动组合下载地址
OPENSRC_VER=${OPENSRC_VER_PREFIX}${OPENSRC_VER_SUFFIX}

# case ${OPENSRC_VER_PREFIX} in
#       5.9 | 5.12 | 5.13 | 5.14 |5.15 )
#       DOWNLOAD_LINK=http://download.qt.io/official_releases/qt/${OPENSRC_VER_PREFIX}/${OPENSRC_VER}/single/${QT_COMPRESS_PACKAGE}
#       ;;
#    *)
#       DOWNLOAD_LINK=http://download.qt.io/new_archive/qt/${OPENSRC_VER_PREFIX}/${OPENSRC_VER}/single/${QT_COMPRESS_PACKAGE}
#       ;;
# esac
# DOWNLOAD_LINK=https://github.com/qt/qtbase/archive/refs/tags/v5.15.16-lts-lgpl.tar.gz
QT_DOWNLOAD_LINK=https://mirrors.ustc.edu.cn/qtproject/archive/qt/5.15/5.15.16/submodules/qtbase-everywhere-opensource-src-5.15.16.tar.xz
#无需修改--自动组合平台路径
QT_CONFIG_PATH=${Qt_SRC_PATH}/${QT_PACKAGE_NAME}/mkspecs/${QT_PLATFORM}

#无需修改--自动组合配置平台路径文件
QT_CONFIG_FILE=${QT_CONFIG_PATH}/qmake.conf

#下载源码包
do_download_qt_src () {
    echo -e "\033[1;33mstart download ${QT_COMPRESS_PACKAGE}...\033[0m"
    
    if [ ! -d "${SRC_PATH}" ] ; then
        mkdir ${SRC_PATH}
    fi
    
    if [ ! -d "${Qt_SRC_PATH}" ] ; then
        mkdir ${Qt_SRC_PATH}
    fi
    
    if [ ! -d "${Qt_SRC_PATH}" ] ; then
        mkdir ${Qt_SRC_PATH}
    fi
    
    cd ${Qt_SRC_PATH}
    
    
    if [ ! -f "${QT_COMPRESS_PACKAGE}" ];then
        if [ ! -d "${QT_PACKAGE_NAME}" ];then
            wget -c ${QT_DOWNLOAD_LINK}
        fi
    fi
    
    echo -e "\033[1;33mdone...\033[0m"
}

#解压源码包
do_tar_qt_package () {
    echo -e "\033[1;33mstart unpacking the ${QT_COMPRESS_PACKAGE} package ...\033[0m"
    if [ ! -d "${QT_PACKAGE_NAME}" ];then
        tar -xf ${QT_COMPRESS_PACKAGE}
    fi
    echo -e "\033[1;33mdone...\033[0m"
    cd ${QT_PACKAGE_NAME}
}

#安装依赖项
do_install_config_qt_dependent () {
    sudo chmod +777 /opt
    echo 123456
    sudo sudo apt install libxcb-cursor0 libxcb-cursor-dev -y
    sudo apt install gcc g++ make ninja-build cmake python3 pkg-config -y
}

#修改配置平台
do_config_qt_before () {
    echo -e "\033[1;33mstart configure QT_PLATFORM...\033[0m"
    
    if [ ! -d "${QT_CONFIG_PATH}" ];then
        cp -a ${Qt_SRC_PATH}/${QT_PACKAGE_NAME}/mkspecs/linux-arm-gnueabi-g++ ${QT_CONFIG_PATH}
    fi
    
    echo "#" > ${QT_CONFIG_FILE}
    echo "# qmake configuration for building with arm-linux-gnueabi-g++" >> ${QT_CONFIG_FILE}
    echo "#" >> ${QT_CONFIG_FILE}
    echo "" >> ${QT_CONFIG_FILE}
    echo "MAKEFILE_GENERATOR      = UNIX" >> ${QT_CONFIG_FILE}
    echo "CONFIG                 += incremental" >> ${QT_CONFIG_FILE}
    echo "QMAKE_INCREMENTAL_STYLE = sublib" >> ${QT_CONFIG_FILE}
    
    echo "" >> ${QT_CONFIG_FILE}
    echo "include(../common/linux.conf)" >> ${QT_CONFIG_FILE}
    echo "include(../common/gcc-base-unix.conf)" >> ${QT_CONFIG_FILE}
    echo "include(../common/g++-unix.conf)" >> ${QT_CONFIG_FILE}
    
    
    
    echo "" >> ${QT_CONFIG_FILE}
    echo "# modifications to g++.conf" >> ${QT_CONFIG_FILE}
    echo "QMAKE_CC                = ${CROSS_PREFIX}gcc " >> ${QT_CONFIG_FILE}
    echo "QMAKE_CXX               = ${CROSS_PREFIX}g++ " >> ${QT_CONFIG_FILE}
    echo "QMAKE_LINK              = ${CROSS_PREFIX}g++ " >> ${QT_CONFIG_FILE}
    echo "QMAKE_LINK_SHLIB        = ${CROSS_PREFIX}g++ " >> ${QT_CONFIG_FILE}
    
    echo "" >> ${QT_CONFIG_FILE}
    echo "QMAKE_CFLAGS            += --sysroot=${SYS_ROOT_PATH}" >> ${QT_CONFIG_FILE}
    echo "QMAKE_CXXFLAGS          += --sysroot=${SYS_ROOT_PATH}" >> ${QT_CONFIG_FILE}
    echo "QMAKE_LFLAGS            += --sysroot=${SYS_ROOT_PATH}" >> ${QT_CONFIG_FILE}
    
    echo "" >> ${QT_CONFIG_FILE}
    echo "# modifications to linux.conf" >> ${QT_CONFIG_FILE}
    echo "QMAKE_AR                = ${CROSS_PREFIX}ar cqs" >> ${QT_CONFIG_FILE}
    echo "QMAKE_OBJCOPY           = ${CROSS_PREFIX}objcopy" >> ${QT_CONFIG_FILE}
    echo "QMAKE_NM                = ${CROSS_PREFIX}nm -P" >> ${QT_CONFIG_FILE}
    echo "QMAKE_STRIP             = ${CROSS_PREFIX}strip" >> ${QT_CONFIG_FILE}
    echo "load(qt_config)" >> ${QT_CONFIG_FILE}
    echo "" >> ${QT_CONFIG_FILE}
    echo "QMAKE_INCDIR=${QT_INSTALL_PATH}/include" >> ${QT_CONFIG_FILE}
    echo "QMAKE_LIBDIR=${QT_INSTALL_PATH}/lib" >> ${QT_CONFIG_FILE}
    
    cat ${QT_CONFIG_FILE}
    echo -e "\033[1;33mdone...\033[0m"
}


#配置选项
do_configure_qt () {
    echo -e "\033[1;33mstart configure ${QT_PACKAGE_NAME}...\033[0m"
    
    export CC=${CROSS_PREFIX}gcc
    export CXX=${CROSS_PREFIX}g++
    #OPENSSL_LIBS='-L${OPENSSL_PATH}/lib -I${OPENSSL_PATH}/include -lssl -lcrypto'
    #PKG_CONFIG_LIBDIR=${PKG_QT_CONFIG_PATH}
    ./configure \
    -prefix ${QT_INSTALL_PATH} \
    -xplatform ${QT_PLATFORM} \
    -release \
    -ssl \
    -sysroot ${SYS_ROOT_PATH} \
    -opensource \
    -confirm-license \
    -shared \
    -no-compile-examples \
    -no-opengl \
    -no-xcb \
    -no-eglfs \
    -v \
    
    #-pkg-config \
    #-list-libraries
    #-openssl-linked \
    
    
    echo -e "\033[1;33mdone...\033[0m"
}

#编译并且安装
do_make_install_qt () {
    echo -e "\033[1;33mstart make and install ${QT_PACKAGE_NAME} ...\033[0m"
    time(make -j6 && make install -j6 )
    echo -e "\033[1;33mdone...\033[0m"
}

#复制openssl qt
do_copy_openssl_to_qt()
{
    echo -e "\033[1;33mstart 复制libssl ...\033[0m"
    cd ${OPENSSL_SRC_PATH}/${OPENSSL_PACKAGE_NAME}
    
    if [ -d "${SYS_ROOT_PATH}${QT_INSTALL_PATH}" ] ;then
        echo -e "cp libcrypto.so* libssl.so* ${SYS_ROOT_PATH}${QT_INSTALL_PATH}/lib ...\033[0m"
        cp libcrypto.so* libssl.so* ${SYS_ROOT_PATH}${QT_INSTALL_PATH}/lib/ -a
    else
        echo -e "cp libcrypto.so* libssl.so* ${HOST_QT_PATH}/lib/ ...\033[0m"
        cp libcrypto.so* libssl.so* ${HOST_QT_PATH}/lib/ -a
    fi
    
    echo -e "\033[1;33m  all done...\033[0m"
}



######################## QT MQTT ##########################

QT_MQTT_SRC_PATH=${SRC_PATH}/QtMqtt
QT_MQTT_PACKAGE_NAME=qtmqtt-5.15.2
QT_MQTT_COMPRESS_NAME=v5.15.2.tar.gz
QT_MQTT_DOWNLOAD_URL=https://github.com/qt/qtmqtt/archive/refs/tags/v5.15.2.tar.gz
# ver5.15.2
do_cross_make_qtmqtt () {
    
    echo -e "\033[1;33mstart make and install QtMqtt ...\033[0m"
    if [ ! -d "${SRC_PATH}" ];then
        mkdir ${SRC_PATH}
    fi

    if [ ! -d "${QT_MQTT_SRC_PATH}" ];then
        mkdir ${QT_MQTT_SRC_PATH}
    fi
    
    cd ${QT_MQTT_SRC_PATH}
    
    if [ ! -f "${QT_MQTT_COMPRESS_NAME}" ];then
        wget -c ${QT_MQTT_DOWNLOAD_URL}
    fi
    
    if [ ! -d "${QT_MQTT_PACKAGE_NAME}" ];then
        tar -xf ${QT_MQTT_COMPRESS_NAME}
    fi
    
    cd ${QT_MQTT_SRC_PATH}/${QT_MQTT_PACKAGE_NAME}
    #复制文件到 /opt/qt5 或 sysroot/opt/qt5
    echo -e "\033[1;33m 复制mqtt 头文件Qt5 include/QtMqtt ...\033[0m"
    if [ -d "${SYS_ROOT_PATH}${QT_INSTALL_PATH}" ] ;then
        if [ ! -d "${SYS_ROOT_PATH}${QT_INSTALL_PATH}/include/QtMqtt" ] ;then
            mkdir ${SYS_ROOT_PATH}${QT_INSTALL_PATH}/include/QtMqtt
        fi
        cp ${QT_MQTT_SRC_PATH}/${QT_MQTT_PACKAGE_NAME}/src/mqtt/*.h ${SYS_ROOT_PATH}${QT_INSTALL_PATH}/include/QtMqtt
        ${SYS_ROOT_PATH}${QT_INSTALL_PATH}/bin/qmake -spec ${QT_PLATFORM}
    else
        mkdir ${QT_INSTALL_PATH}/include/QtMqtt
        cp ./src/mqtt/*.h ${QT_INSTALL_PATH}/include/QtMqtt
        ${QT_INSTALL_PATH}/bin/qmake -spec ${QT_PLATFORM}
    fi
    make -j6
    make install
    
    echo -e "\033[1;33m Mqtt done...\033[0m"
}

do_copy_qt() {
    echo -e "\033[1;33m 复制Qt开始...\033[0m"
    echo -e "\033[1;33m 删除/opt/Qt5...\033[0m"
    rm -rf  ${QT_INSTALL_PATH}
    echo -e "\033[1;33m 复制Qt到/opt...\033[0m"
    cp -R -f -p ${SYS_ROOT_PATH}${QT_INSTALL_PATH} /opt
    echo -e "\033[1;33m   done...\033[0m"
}


set -e
do_make_src_dir

#openssl 编译安装
do_download_openssl
do_config_openssl
do_make_openssl

#qt编译安装
do_download_qt_src
do_tar_qt_package
do_install_config_qt_dependent
do_config_qt_before
do_configure_qt
do_make_install_qt

#复制openssl到qt
do_copy_openssl_to_qt

#mqtt 编译安装
do_cross_make_qtmqtt

#复制完整版qt 到 /opt
do_copy_qt
